<?php

define('ACC',true);

require ('../includes/init.php');
require ('left.php');
$index=new IndexModel();

if(isset($_SESSION['user_id']) && isset($_SESSION['username'])){
	$smarty->assign('SESSION', $_SESSION);
}
if(!isset($_GET['act'])){
	
	$list=$index->getList();
	$cat_list=$index->catSort($list);
	
	$bestList=$index->getTop('best');
	$hotList=$index->getTop('hot');
	$newList=$index->getTop('new');
	
	$smarty->assign("cat_list",$cat_list);
	$smarty->assign("bestList",$bestList);
	$smarty->assign("hotList",$hotList);
	$smarty->assign("newList",$newList);
	$smarty->display("./front/index.html");
	
}else if(isset($_GET['act']) && ($_GET['act'] == 'goodsbox')){
	
	$cat_id = $_GET['cat_id'] + 0;
	$property = trim($_GET['property']);
	$goodsbox = $index->goodsbox($cat_id, $property);
	echo json_encode($goodsbox);
	return ;

}else if(isset($_GET['act']) && ($_GET['act'] == 'search')){
	$category = new CategoryModel();
	
	if(isset($_GET['sea'])){
		$search = trim($_GET['sea']);
	}else{
		$search = '';
	}
	
	$keywords = trim($_POST['keywords']);
	
	$rowCount = $index->getRowCount2($keywords);
	$pageSize = 1;
	
	$category->setMessage($rowCount, $pageSize);
	$pageCount = $category->getPageCount();
	
	if($pageCount <= 5){
		$category->setMaxPageSize($pageCount);
	}else{
		$category->setMaxPageSize(5);
	}
	
	if(isset($_POST['pageNow'])){
			
		$pageNow = $_POST['pageNow'] + 0;//echo $pageNow;exit;
		if($pageNow <= 0){
			$pageNow = 1;
		}else if($pageNow > $pageCount){
			$pageNow = $pageCount;
		}
	}else{
		$pageNow = 1;
	}
	
	$category->setPageNow($pageNow);
	
	$navigate = $category->getFenyeCode();
	
	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);
	
	if(isset($_POST['sort'])){
	
		$sort = trim($_POST['sort']);
		$sort_value = $sort;
		$sort = explode("+", $sort);
		if(!isset($sort[0])){
			$sort[0] = '';
		}else if(!isset($sort[1])){
			$sort[1] = '';
		}
	
		$goods = $index->get_goods_sort($keywords, $sort, $pageNow, $pageSize);
		
		$smarty->assign('goods', $goods);
		$smarty->assign('sort_value', $sort_value);
		$smarty->assign('sort', $sort);
	
	}else{
	
		$goods = $index->goodsSearch($keywords, $pageNow, $pageSize);
			
		$smarty->assign('goods', $goods);
	
	}
	/* if(isset($sort[0]) && $sort[0] != '' && isset($sort[1]) && $sort[0] != ''){
			
		$sort = trim($_POST['sort']);
		$sort_value = $sort;
		$sort = explode("+", $sort);
			
		$goods = $index->get_goods_sort($keywords, $sort, $pageNow, $pageSize);
		$smarty->assign('goods', $goods);
		$smarty->assign('sort_value', $sort_value);
		$smarty->assign('sort', $sort);
			
	}else{
		$goods = $index->goodsSearch($keywords, $pageNow, $pageSize);
			
		$smarty->assign('goods', $goods);
	} */
	
	$smarty->assign('keywords', $keywords);
	$smarty->assign("goods",$goods);
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>搜索');
	$smarty->assign('search', $search);
	$smarty->assign('action', 'search');
	$smarty->display('./front/goodsbox.html');
	
}











?>